import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:guochaoapp/common/widges/appbar.dart';
import 'package:guochaoapp/model/cart_settle_entity.dart';

import '../../r.dart';

class GCShoppingListPage extends StatelessWidget {
  var _listData = RxList<CartSettleDataItemList>.empty();

  @override
  Widget build(BuildContext context) {
    _listData.assignAll(Get.arguments);
    return Scaffold(
        appBar: GCAppBar(titleText: "果巢商城清单"),
        body: Obx(() => _initSubviews()));
  }

  _initSubviews() {
    return ListView.builder(
        itemCount: _listData.length,
        itemBuilder: (BuildContext context, int index) {
          return _getListView(index);
        });
  }

  _getListView(int index) {
    CartSettleDataItemList item = _listData[index];
    double childHeight = item.itemList.length * 60.0;
    return Column(children: [
      Container(
          color: Color(0xFF40A8B3),
          height: ScreenUtil().setSp(52),
          margin: EdgeInsets.only(
              left: ScreenUtil().setSp(8),
              right: ScreenUtil().setSp(8),
              top: ScreenUtil().setSp(2)),
          child: Row(
            children: [
              SizedBox(width: ScreenUtil().setSp(14)),
              Text(
                item.categoryName + " | ",
                style: TextStyle(
                    color: Colors.white, fontSize: ScreenUtil().setSp(16)),
              ),
              Opacity(
                opacity: 0.69,
                child: Text(
                  "共${item.itemNum}件",
                  style: TextStyle(
                      color: Colors.white, fontSize: ScreenUtil().setSp(14)),
                ),
              ),
              Expanded(child: SizedBox()),
              Text(
                "￥${item.itemPrice}",
                style: TextStyle(
                    color: Colors.white, fontSize: ScreenUtil().setSp(16)),
              ),
              GestureDetector(
                  onTap: () => _expand(item),
                  child: Container(
                    height: 60,
                    width: 60,
                    color: Colors.transparent,
                    child: Center(
                      child: Image.asset(
                          item.expand
                              ? R.assetsIconArrowUp
                              : R.assetsIconArrowDown,
                          height: 16,
                          width: 16),
                    ),
                  )),
            ],
          )),
      Offstage(
          offstage: !item.expand,
          child: Container(
              color: Colors.white,
              height: ScreenUtil().setSp(childHeight),
              margin: EdgeInsets.only(
                  left: ScreenUtil().setSp(8), right: ScreenUtil().setSp(8)),
              child: ListView.builder(
                  shrinkWrap: true,
                  physics: ScrollPhysics(),
                  itemCount: item.itemList.length,
                  itemBuilder: (BuildContext context, int index) {
                    return _getListItemView(item, index);
                  }))),
    ]);
  }

  _getListItemView(CartSettleDataItemList data, int index) {
    CartSettleDataItemListItemList item = data.itemList[index];
    return Container(
      height: ScreenUtil().setSp(60),
      child: Column(children: [
        SizedBox(height: ScreenUtil().setSp(9)),
        Row(
          children: [
            SizedBox(width: ScreenUtil().setSp(24)),
            Container(
                padding: EdgeInsets.only(
                    left: ScreenUtil().setSp(2), right: ScreenUtil().setSp(2)),
                color: Color(0xFF40A8B3),
                child: Text(
                  "${item.brandName}",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: ScreenUtil().setSp(12),
                  ),
                )),
            SizedBox(width: ScreenUtil().setSp(4)),
            Text("${item.mallTitle}",
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(12),
                )),
            Expanded(child: SizedBox()),
            Opacity(
                opacity: 0.8,
                child: Text(
                  "单价: ￥${item.price}",
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                  ),
                )),
            SizedBox(width: ScreenUtil().setSp(10)),
          ],
        ),
        SizedBox(height: ScreenUtil().setSp(8)),
        Row(
          children: [
            SizedBox(width: ScreenUtil().setSp(24)),
            Text("规格:${item.skuSpec}",
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                    fontWeight: FontWeight.bold)),
            Expanded(child: SizedBox()),
            Text("X ${item.num}${item.unit}",
                style: TextStyle(
                    fontSize: ScreenUtil().setSp(12),
                    fontWeight: FontWeight.bold)),
            SizedBox(width: ScreenUtil().setSp(12)),
          ],
        )
      ]),
    );
  }

  _expand(CartSettleDataItemList item) {
    item.expand = !item.expand;
    _listData.refresh();
  }
}
